using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Citrix.Cmp;
using System.IO;
using System.Diagnostics;

namespace SkinningSample
{
    /// <summary>
    /// The main app class.
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Application.Current can return null and it is typed to the base Application class.
        /// So we store our own typed ref for accessing the app object.
        /// </summary>
        private static App thisRef;

        /// <summary>
        /// Base CMP .NET API.
        /// </summary>
        private CmpApi cmpApi;

        /// <summary>
        /// WPF extensions fpr CMP.NET.
        /// </summary>
        private CmpWpfApi wpfCmpApi;

        /// <summary>
        /// This is the reference DPI the app was designed for. The DPI scaling is done relative
        /// to this DPI. The styles that come with the original app template were designed for
        /// this DPI.
        /// </summary>
        private const int ReferenceAppDpi = 163;

        /// <summary>
        /// The root object for the view-model layer.
        /// </summary>
        private readonly ViewModel.ViewModelManager vmMgr;

        /// <summary>
        /// Default ctor.
        /// </summary>
        public App()
        {
            thisRef = this;

            this.Exit += new ExitEventHandler(App_Exit);

            this.cmpApi = new CmpApi();
            this.wpfCmpApi = new CmpWpfApi(this.cmpApi, new ReferenceDpi(ReferenceAppDpi, ReferenceAppDpi));
            this.vmMgr = new SkinningSample.ViewModel.ViewModelManager(this);
        }

        /// <summary>
        /// Static, typed app object getter.
        /// </summary>
        public static App TypedApp
        {
            get { return thisRef; }
        }

        /// <summary>
        /// The CMP WPF API object.
        /// </summary>
        internal CmpWpfApi CmpWpfApi
        {
            get { return this.wpfCmpApi; }
        }

        /// <summary>
        /// Returns the base object in the view-model layer.
        /// </summary>
        internal SkinningSample.ViewModel.ViewModelManager ViewModel
        {
            get { return this.vmMgr; }
        }

        /// <summary>
        /// App Exit event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_Exit(object sender, ExitEventArgs e)
        {
            if (null != this.cmpApi)
            {
                this.cmpApi.Dispose();
            }
        }
    }
}
